import QtQuick 2.0

Item {
    id: versionLoader

    signal error()

    readonly property string url: isDebug
            ? "http://localhost:8000/resource.json"
            : "https://sdk-os-static.mihoyo.com/hk4e_global/mdk/launcher/api/resource?key=gcStgarh&launcher_id=10"
    property var json
    property bool isLoading: false

    function loadVersion() {
        isLoading = true
        var xhr = new XMLHttpRequest();
        xhr.onreadystatechange = ((response) => {
            return () => {
                if (response.readyState === 4) {
                    isLoading = false
                    try {
                        onResponseLoaded(JSON.parse(response.responseText))
                    } catch(e) {
                        console.error(e)
                        error()
                    }
                }
            }
        })(xhr)
        xhr.open('GET', url, true);
        xhr.send('');
    }

    function onResponseLoaded(json) {
        versionLoader.json = json
//        console.log(JSON.stringify(json, null, 2))
    }

    Component.onCompleted: console.log(url)
}
